package com.hm.service.request.test;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.entity.AuthorizationParamsBean;
import com.hm.service.http.HttpUtils;
import com.hm.service.utils.EncryptionUtils;
import com.hm.service.utils.JsonUtils;
import com.hm.service.utils.Md5Utils;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;


/**
 * 获取session接口
 */
public class Request01 {

    private static final String url = "http://172.26.2.95:8081/rick/main/000_000_000";
    private static final String secretKey = "kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH";


    public static void main(String[] args) {
        //请求参数
        JSONObject jsonObject = getJsonObject();
        //构建请求体
        RequestBody body = RequestBody.create(jsonObject.toJSONString(), MediaType.get("application/json; charset=UTF-8"));
        Request request = new Request.Builder()//
                .url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        //同步请求
        OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody resBody = response.body();
            if (resBody != null) {
                String jsonString = resBody.string();
                boolean json = JsonUtils.isJson(jsonString);
                System.out.println("1ResponseBody-->:" + jsonString);
                System.out.println("2ResponseBody-->:");
                System.out.println(json ? jsonString : EncryptionUtils.decrypt(secretKey, jsonString));
            } else {
                System.out.println("ResponseBody为空");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static JSONObject getJsonObject() {
        long s = System.currentTimeMillis();
        String timestamp = String.valueOf(s);
        String issue = "1.0.1";
        String version = "3.0.1";
        String channel = "a";
        String author = "hm";
        String permission = "1.0.2";
        String signature = timestamp.toLowerCase() + channel.toLowerCase() + version.toLowerCase() + author.toLowerCase() + permission.toLowerCase() + issue.toLowerCase();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("timestamp", timestamp);
        jsonObject.put("issue", issue);
        jsonObject.put("version", version);
        jsonObject.put("channel", channel);
        jsonObject.put("author", author);
        jsonObject.put("permission", permission);
        jsonObject.put("signature", Md5Utils.generateMD5(signature));
        return jsonObject;
    }
}